<?php
namespace Api\Model;



class MedicalRecordCollectModel extends BaseModel {

    private static $instance;
    const IS_SHOW=1;
    const NOT_SHOW=0;
    public static function singleton()
    {
        self::$instance === null and self::$instance = new self();
        return self::$instance;
    }

    public function collect($user_id,$record_id,$statsu){
        $row= $this->getOne($user_id,$record_id);
        $rst=array();
        if($row['id'] >0){
            $where = array();
            $where['user_id']=$user_id;
            $where['record_id']=$record_id;
            $data = array();
            if($statsu==self::IS_SHOW){
                $data['is_show']=self::IS_SHOW;
            }else{
                $data['is_show']=self::NOT_SHOW;
            }

            $res= $this->where($where)->save($data);
            if($res>0){
                if($statsu==self::IS_SHOW){
                    MedicalRecordModel::singleton()->incCollectCount($record_id);//增加收藏数
                }else{
                    MedicalRecordModel::singleton()->decCollectCount($record_id);//减少收藏数
                }
                $rst['result']=200;
                $rst['msg']='提交成功';
            }else{
                $rst['result']=202;
                $rst['msg']='提交失败';
            }

        }else{
            $data = array();
            $data['user_id']=$user_id;
            $data['record_id']=$record_id;
            $data['is_show']=self::IS_SHOW;
            $data['create_time']=date('Y-m-d H:i:s');
            $res= $this->add($data);
            if($res>0){
                MedicalRecordModel::singleton()->incCollectCount($record_id);
                $rst['result']=200;
                $rst['msg']='提交成功';
            }else{
                $rst['result']=202;
                $rst['msg']='提交失败';
            }
        }
        return $rst;
    }

    public function getOne($user_id,$record_id){
        $where['user_id']=$user_id;
        $where['record_id']=$record_id;
        return $this->where($where)->find();
    }

    public function getRecordIdsByUserId($user_id){
        $where['user_id']=$user_id;
        $where['is_show']=self::IS_SHOW;
        $rows = $this->where($where)->select();
        $record_ids=array();
        foreach($rows as $key=>$val){
            $record_ids[]=$val['record_id'];
        }
        return $record_ids;
    }
    //判断用户是否收藏病历
    public function isCollect($user_id,$record_id){
        $where['user_id']=$user_id;
        $where['record_id']=$record_id;
        $where['is_show']=1;
        $res = $this->where($where)->find();
        if($res){
            return 1;
        }else{
            return 0;
        }
    }
}